package com.wl.ms.seckill.controller;

import com.wl.ms.seckill.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author wanlei
 * @since 2021/9/24
 */
@RestController
public class SeckillController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/createWrongOrder/{sid}")
    @ResponseBody
    public String createWrongOrder(@PathVariable int sid) {
        int id = 0;
        try {
            id = orderService.createWrongOrder(sid);
        } catch (Exception e) {
            System.out.println("出错了");
            e.printStackTrace();
        }
        return String.valueOf(id);
    }

    public String createOptimisticOrder(@PathVariable int sid) {
        int id;
        try {
            id = orderService.createOptimisticOrder(sid);
            System.out.println("购买成功，剩余库存为:"+ id);
        } catch (Exception e) {
            System.out.println("购买失败：[{}]"+e.getMessage());
            return "购买失败，库存不足";
        }
        return String.format("购买成功，剩余库存为：%d", id);
    }
}
